<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      // function getAverageAge(a = []) {
      //   let sum = 0;
      //   a.forEach((el) => (sum += el.age));//和
      //   return sum / a.length;
      // }
      function getAverageAge(a = []) {
        const r = a.reduce((sum, v) => {
          return sum + v.age;
        }, 0);
        return r / a.length;
      }
      let arr = [
        { name: "John", age: 25 },
        { name: "Pete", age: 30 },
        { name: "Mary", age: 28 },
      ];
      console.log(getAverageAge(arr));
    </script>
  </body>
</html>
